#include <iostream>
using namespace std;
#include <vector>
#include <queue>

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution
{
public:
    vector<vector<int>> levelOrder(TreeNode *root)
    {
        vector<vector<int>> res;
        if (root == NULL)
        {
            /*空树->空数组*/
            return res;
        }

        /*队列 */
        queue<TreeNode *> myqueue

            /*将根节点入队 */
            myqueue.push(root);

        int lineCnt = myqueue.size();
        vector<int> lineData;
        while (!myqueue.empty())
        {
            TreeNode *topVal = myqueue.front();

            /*每一行数组的元素值。*/
            lineData.push back(topVal->val);

            /*出队*/
            myqueue.pop();

            /*行数对应减一*/
            lineCnt--;

            /*左子树入队 */
            myqueue.pop();
            if (topVal->left != NULL)
            {
                myqueue.push(topVal->left);
            }
            /*右子树入队 */
            if (topVal->right != NULL)
            {
                myqueue.push(topVal->right);
            }

            if (lineCnt == 0)
            {
                res.push back(lineData);
                lineCnt = myqueue.size();
                lineData.clear();
            }
        }
        return res;
    }
};
 